'''
Fans User's method
'''
import MySQLdb
import types
from WeiboClient import WeiboClient
from MySqlDBHandle import MySqlDBHandle
from WeiboAcc import WeiboAcc

class FollowerUser(WeiboClient):
    mSQLStatement = "INSERT INTO LU_Follower (Follower_id, City_id, genderID, isVerified, cFollower_Post, cFollower_Fans, LAST_UPDATE_TIMESTAMP)\
	    VALUES %s ON DUPLICATE KEY UPDATE LAST_UPDATE_TIMESTAMP = current_timestamp, cFollower_Post = Values(cFollower_Post), cFollower_Fans =\
	    Values(cFollower_Fans);"
    mSQLValueStatement = "(%s, %s, '%s', %s, %s, %s, current_timestamp),"

    mAPI = "friendships/followers"

    mWeiboAccData = []
    mFollowerIdList = []

    #Temp Function to insert city info to LU_City
    mSQLCityStatement = "INSERT INTO LU_City (City_id, City_name, Province_id, LAST_UPDATE_TIMESTAMP)\
	    VALUES (%s, 'unknown', %s, current_timestamp) ON DUPLICATE KEY UPDATE LAST_UPDATE_TIMESTAMP = current_timestamp;"

    def __init__(self, iUid):
        self.mUid = iUid

        WeiboClient.__init__(self)

    def _sendToDB(self, iJsonData):
        assert(type(iJsonData) == types.ListType)
        lValueStatement = ""
        self.mFollowerIdList = []
        for lFollower in iJsonData:
            lProvinceId = lFollower['province'].zfill(3)
            lCityId = "001" + lProvinceId + lFollower['city'].zfill(3)
            lValueStatement += (self.mSQLValueStatement % (lFollower['id'], lCityId, \
    		    lFollower['gender'], lFollower['verified'], lFollower['statuses_count'], lFollower['followers_count']))
            
            #Temp Function to insert city info to LU_City
            if lFollower['city'] == '1000':
                lSQLCityStatement = self.mSQLCityStatement % (lCityId, lProvinceId)
                try:
                    MySqlDBHandle.execSqlStatement(lSQLCityStatement)
                except Exception, e:
                    print ("Error when insert the city = %s , Province = %s into LU_City Table" % (lCityId, lProvinceId))
            
            iSingleWeiboAccData = {} 
            iSingleWeiboAccData['id'] = lFollower['id']
            iSingleWeiboAccData['screen_name'] = lFollower['screen_name']
            iSingleWeiboAccData['name'] = lFollower['name']
            iSingleWeiboAccData['followers_count'] = lFollower['followers_count']
            self.mWeiboAccData.append(iSingleWeiboAccData)
            self.mFollowerIdList.append(lFollower['id'])
        
        try:
            weiboAccObj = WeiboAcc(self.mWeiboAccData)
            weiboAccObj.process()
        except Exception, e:
            print "Error in FollowerUse.py when insert weiboacc to db"
        
        lSQLStatement = self.mSQLStatement % lValueStatement[:len(lValueStatement) - 1]
    
        try:
            MySqlDBHandle.execSqlStatement(lSQLStatement)
        except Exception, e:
            print "Error in FollowerUser.py sendToDB function with error_message = " + str(e)
                
    def process(self):
        lParams = {}
        lParams['uid'] = self.mUid
        lParams['access_token'] = self.mPublicToken[1]
        lParams['count'] = 50
        lParams['total_number'] = 3000
        
        print ("Get followers of UserId = %s from Sina Weibo ..." % self.mUid)
        lJsonResult = self.fetchUsingAPI(self.mAPI, lParams)
        if type(lJsonResult) == types.ListType and len(lJsonResult) > 0:
            self._sendToDB(lJsonResult)
        print ("Insert followers info into LU_Follower table UserId = %s success !!!" % self.mUid)
            
    def getFollowersList(self):
        return self.mFollowerIdList
    
    

